/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.wii.elpro.web.action;

import com.wii.elpro.dao.QuotationDAOInterface;
import com.wii.elpro.dao.hb.QuotationDAOImpl;
import com.wii.elpro.dao.HibernateUtil;
import com.wii.elpro.entity.Quotation;
import com.wii.elpro.web.form.QuotationUpdateForm;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author hp02
 */
public class QuotationUpdateAction extends DispatchAction {
    
    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String forward;
        long id = Long.parseLong(request.getParameter("id"));
        QuotationDAOInterface dao = new QuotationDAOImpl();
        QuotationUpdateForm f = (QuotationUpdateForm) form;
        ActionMessages messages = null;
        HttpSession sess = request.getSession(true);
        try {
            Quotation quotation = dao.getQuotation(id);
            f.setDelivery(quotation.getDelivery());
            f.setId(quotation.getId());
            f.setLengt(quotation.getLength());
            f.setLoco(quotation.getLoco());
            f.setMatrial(quotation.getMatrial());
            f.setNoQuotation(quotation.getNoQuotation());
            f.setPaymentTerm(quotation.getPaymentTerm());
            f.setPositionMatrial(quotation.getPositionMatrial());
            f.setPrice(quotation.getPrice());
            f.setSpdcf(quotation.getSpdcf().getId());
            f.setStatus(quotation.getStatus());
            f.setStatusDesc(quotation.getStatusDesc());
            f.setThickness(quotation.getThickness());
            f.setValidityPrice(quotation.getValidityPrice().toString());
            f.setValidityStock(quotation.getValidityStock());
            f.setVat(quotation.getVat());
            request.setAttribute("date", quotation.getTglQuotation());
            request.setAttribute("spdcf", quotation.getSpdcf());
            forward = "update";
        } catch (Exception ex) {
            messages = new ActionMessages();
            messages.add("message", new ActionMessage("customMessage", "Application is fail to retrieve Project Owner data. Please contact my developer."));
            saveMessages(sess, messages);
            ex.printStackTrace();
            forward = "gotomain";
        }
        return mapping.findForward(forward);
    }
    
    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        String forward;
        QuotationDAOInterface dao = new QuotationDAOImpl();
        QuotationUpdateForm f = (QuotationUpdateForm) form;
        Quotation quotation = new Quotation();
        ActionMessages messages = new ActionMessages();
        HttpSession sess = request.getSession(true);
        try {
            quotation.setSpdcf(dao.getQuotation(f.getId()).getSpdcf());
            quotation.setDelivery(f.getDelivery());
            quotation.setId(f.getId());
            quotation.setLength(f.getLengt());
            quotation.setLoco(f.getLoco());
            quotation.setMatrial(f.getMatrial());
            quotation.setNoQuotation(f.getNoQuotation());
            quotation.setPaymentTerm(f.getPaymentTerm());
            quotation.setPositionMatrial(f.getPositionMatrial());
            quotation.setPrice(f.getPrice());
            quotation.setStatus(f.getStatus());
            quotation.setStatusDesc(f.getStatusDesc());
            quotation.setThickness(f.getThickness());
            quotation.setItems(dao.getQuotation(f.getId()).getItems());
            DateFormat formatter;
            formatter = new SimpleDateFormat("yyyy-MM-d");
            if(f.getValidityPrice()!=null){
                quotation.setValidityPrice(formatter.parse(f.getValidityPrice()));
            }
            quotation.setTglQuotation(dao.getQuotation(f.getId()).getTglQuotation());
            quotation.setValidityStock(f.getValidityStock());
            quotation.setVat(f.getVat());
            // i have to clear the session before invoke update method. The other way can be done by using merge() method instead update().
            HibernateUtil.getSession().clear(); // what is the consequences of this way ??
            dao.update(quotation);
            messages.add("customMessage", new ActionMessage("customMessage", "Your data is successfully updated."));
            forward = "gotomain";
        } catch (Exception ex) {
            messages.add("message", new ActionMessage("customMessage", "Application is fail to update your data. Please contact my developer."));
            ex.printStackTrace();
            forward = "fail";
        }
        saveMessages(sess, messages);
        return mapping.findForward(forward);
    }
}
